var xmlHttp;
function createXMLHttpRequest() {
    if(window.XMLHttpRequest) {
    	xmlHttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {
    	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
}
//////////////////////////////////
function startRequest() {
	createXMLHttpRequest();
	
	var queryString = "name=korpton&age=30";
	
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("GET", "simpleResponse.txt");
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //json
	//xmlHttp.setRequestHeader("Content-Type", "text/xml"); //xml
	xmlHttp.send(queryString);
}
///////////////////////////////////
function createQueryString() {
	return "name=" + document.getElementById("namefield").value;
}
function doGetRequest() {
	var url = "ajaxServlet?" + createQueryString();
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("GET", url);
	xmlHttp.send(null);
}
function doPostRequest() {
	var url = "ajaxServlet?timeStamp=" + new Date().getTime(); 
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("POST", url);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(createQueryString());
}
///////////////////////
function prepareXML() {
    var xml = "<skills>";
    var options = document.getElementById("skills").childNodes;
    for(var i = 0; i < options.length; i++) {
    	var option = options[i];
    	if(option.selected) {
    		xml = xml + "<skill>" + option.value + "<\/skill>";
    	}
    }
    xml = xml + "<\/skills>";
    return xml;
}

function handleSkills() {
    var xml = prepareXML();
    var url = "ajaxServlet?timeStamp" + new Date().getTime();
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("POST", url);
    xmlHttp.setRequestHeader("Content-Type", "text/xml");
    xmlHttp.send(xml);
}
/////////////// json /////////////////////
function Account(number, name, balance) {
    this.number = number;
    this.name = name;
    this.balance = balance;
}

function AccountJson(){
	var account = new Account(123456, "Justin", 1000);
	alert(account.toJSONString());
}


function prepareJSON() {
	var number = document.getElementById("num").value;
	var name = document.getElementById("name").value;
	var balance = document.getElementById("bal").value;
	
	var account = new Object();
	account.number = number;
	account.name = name;
	account.balance = balance;
	
	return account.toJSONString();
}

function handleAccount() {
	
    //var json = prepareJSON();
    var json = new Account(123456, "Justin", 1000);
	var url = "ajaxServlet?timeStamp" + new Date().getTime();
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("POST", url);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(json);
}

////////////////////////
	
function handleStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			//alert("Server Reponse:" + xmlHttp.responseText);
			document.getElementById("response").innerHTML = xmlHttp.responseText;
		}
	}
}